import { createRouter, createWebHistory, type RouteRecordRaw } from "vue-router"

const routes: RouteRecordRaw[] = [
  {
    path: "/",
    redirect: "/home"
  },
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/login/Login.vue")
  },
  {
    path: "/home",
    name: "main",
    component: () => import("@/views/main/Main.vue"),
    children: [
      {
        path: "/home",
        name: "home",
        component: () => import("@/views/main/home.vue")
      }
    ]
  },
  {
    path: "/:pathMatch(.*)*",
    name: "not-found",
    component: () => import("@/views/not-found/not-found.vue")
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

// 导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === "/login" || localStorage.getItem("token")) {
    next()
  } else {
    next("/login")
  }
  console.log(router.getRoutes())
  // console.table([to.path, from.path])
})

export default router
